$(function(){
	/*
	 * 初始化 tooltip
	 */
	$('.modal').modal({
		backdrop: 'static',
		show: false
	});
	
	function FormatDate (strTime) {
	    var date = new Date(strTime);
	    return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+ date.toLocaleTimeString();
	}
	
	var temp= "<tr data-event-notice-id=''><td class='widthId'></td><td class='widthContent'></td><td class='widthTime'></td><td class='widthOperation'></td><td><a href='javascript:;' id='modifyBtn' class='btn btn-xs btn-success btn-edit-notice'>编辑</a> <a href='javascript:;' id='deleteBtn' class='btn btn-xs btn-danger btn-del-notice'>删除</a></td></tr>";

	//上传图片
	var $coverUploading = $('#cover-upload-box .overlay').hide();
	var uploader = new PicUploader({
		success : function(obj) {
			var picUrl = obj['data'];
			$coverUploading.hide();
			$('[name="coverPic"]').val(picUrl);
			$('.thumbnail').css('background-image', 'url(' + picUrl + ')');
		}
	});
	$('.thumbnail').on('click', function() {
		uploader.start();
		$coverUploading.click();
	});
	$('#cover-pic').on('change',function(){
		var picFormData = new FormData();
		picFormData.append('file', this.files[0]);
		$coverUploading.show();
		$.ajax({
			url : '/base/upload/uploadFile',
			type : 'post',
			data : picFormData,
			dataType : 'json',
			cache : false,
			contentType : false,
			processData : false,
			success : function(obj) {
				if (obj.errorCode == 0) {
					$coverUploading.hide();
					$('.thumbnail').css('background-image',
							'url(' + obj.data + ')');
					$('#coverPicUrlInput').val(obj.data);
				} else {
				}
			}
		});
	});
	//上传图片
	
	
	$('#add-notice').on('click',function(){
		$('#id').val("");
		$('#coverPicUrlInput').val("");
		$('#noticeContent').val("");
		$('.thumbnail').css('background-image','');
		$("#notice-modal").modal('show');
	});
	$('tbody').on('click','.btn-del-notice',function(){
		var $tr= $(this).parents('tr');
		actionConfirm({
			msg:"删除后将无法恢复，确认删除吗？",
			confirm:function(){
				cAjax({
					url:'/cms/event/notice/del',
					data:{'id':$tr.data('event-notice-id')},
					success:function(obj){
						$tr.remove();
					}
				});
			}
		});
	});
	$('tbody').on('click','.btn-edit-notice',function(){
		$('#id').val("");
		$('#coverPicUrlInput').val("");
		$('#noticeContent').val("");
		cAjax({
			url:'/cms/event/notice/get',
			data:{"id":$(this).parents('tr').data('event-notice-id')},
			success:function(obj){
				$('#id').val(obj.data.id);
				$('#coverPicUrlInput').val(obj.data.image);
				$('#noticeContent').val(obj.data.content);
				$('.thumbnail').css('background-image',
						'url(' + obj.data.image + ')');
				$("#notice-modal").modal('show');
			}
		});
		
		
	});
	$('#btn-save-notice').on('click',function(){
		var data = {};
		var url = '/cms/event/notice/add';
		var $tr = "";
		if($('#id').val()!=""){
			data['id'] =  $('#id').val();
			url = '/cms/event/notice/edit';
			$tr = $("tr[data-event-notice-id='"+data['id']+"']");
		}
		data['content'] = $('#noticeContent').val();
		data['image'] = $('#coverPicUrlInput').val();
		data['eventId'] = $('#eventId').val();
		cAjax({
			url:url,
			data:data,
			success:function(obj){
				if($tr==""){
					$('tbody').prepend(temp);
					$tr = $('tbody tr:first');
					$tr.find('td:eq(2)').html(FormatDate(obj.data.createTime));
				}
				$tr.attr('data-event-notice-id',obj.data.id);
				$tr.find('td:eq(0)').html(obj.data.id);
				$tr.find('td:eq(1)').html(obj.data.content);
				$tr.find('td:eq(3)').html($("#eventTitle").val());
				$("#notice-modal").modal('hide');
			}
		});
	});
	
});
